
package jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import jp.co.withone.osgi.gadget.bundlecontroller.manager.ViewManager;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;

/**
 *
 * @author takada
 */
public class InstallView extends javax.swing.JFrame {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1655265067442150599L;

    private static final String KEY_MESSAGE_TITLE = "installView.message.title";
    private static final String KEY_MESSAGE_LABEL = "installView.message.label";
    private static final String KEY_MESSAGE_FILECHOOSEBUTTON = "installView.message.fileChooseButton";
    private static final String KEY_MESSAGE_INSTALLBUTTON = "installView.message.installButton";
    private static final String KEY_MESSAGE_CANCELBUTTON = "installView.message.cancelButton";
    private static final String KEY_MESSAGE_DIALOG_INSTALL_ERROR_TITLE ="installView.error.install.dialog.title";
    private static final String KEY_MESSAGE_DIALOG_INSTALL_ERROR_MESSAGE ="installView.error.install.dialog.message";
    private static final String KEY_MESSAGE_DIALOG_INPUT_ERROR_TITLE ="installView.error.input.dialog.title";
    private static final String KEY_MESSAGE_DIALOG_INPUT_ERROR_MESSAGE ="installView.error.input.dialog.message";
    
    
    private ViewManager manager = null;
    
    private JButton fileChooseButton = null;
    private JButton installButton = null;
    private JButton cancelButton = null;
    private JLabel label = null;
    private JSeparator separator = null;
    private JTextField textField = null;

    public InstallView(ViewManager manager) {
        this.manager = manager;
        initComponents();
    }

    /**
     * 
     */
    private void initComponents() {

        textField = new JTextField();
        textField.addFocusListener(new TextFieldFocusListener());
        fileChooseButton = new JButton();
        fileChooseButton.addActionListener(new FileChooseActionListener());
        label = new JLabel();
        installButton = new JButton();
        installButton.addActionListener(new InstallActionListener());
        cancelButton = new JButton();
        cancelButton.addActionListener(new CancelActionListener());
        separator = new JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
//        setBackground(new java.awt.Color(255, 255, 255));
        
        setTitle(manager.getMessage(KEY_MESSAGE_TITLE));
        fileChooseButton.setText(manager.getMessage(KEY_MESSAGE_FILECHOOSEBUTTON));
        label.setText(manager.getMessage(KEY_MESSAGE_LABEL));
        installButton.setText(manager.getMessage(KEY_MESSAGE_INSTALLBUTTON));
        cancelButton.setText(manager.getMessage(KEY_MESSAGE_CANCELBUTTON));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(separator, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(installButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cancelButton))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fileChooseButton))))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fileChooseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(installButton))
                .addContainerGap())
        );
        pack();
    }
    
    private InstallView getInstallView(){
        return this;
    }
    private class FileChooseActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser(textField.getText());
            chooser.setFileHidingEnabled(false);
            int returnVal = chooser.showOpenDialog(chooser);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                java.io.File file = chooser.getSelectedFile();
                textField.setText("file:" + file.getAbsolutePath());
            }
        }
        
    }
    
    private class CancelActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    }
    
    private class InstallActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Bundle bundle = manager.getBundleContext().installBundle(textField.getText());
                manager.displayBundleControledMessage(BundleActionCommand.INSTALL, bundle);
                dispose();
            } catch (BundleException e1) {
                JOptionPane.showMessageDialog((JComponent)e.getSource(), manager.getMessage(KEY_MESSAGE_DIALOG_INSTALL_ERROR_MESSAGE), manager.getMessage(KEY_MESSAGE_DIALOG_INSTALL_ERROR_TITLE), JOptionPane.ERROR_MESSAGE);
                e1.printStackTrace();
            }
            
        }
    }
    
    private class TextFieldFocusListener implements FocusListener {

        @Override
        public void focusGained(FocusEvent e) {
            if(textField.getText() == null || textField.getText().length() == 0){
                textField.setText("file:");
            }
        }

        @Override
        public void focusLost(FocusEvent e) {
            try {
                // Check URL.
                new URL(textField.getText());
            } catch (MalformedURLException e1) {
                JOptionPane.showMessageDialog(getInstallView(), manager.getMessage(KEY_MESSAGE_DIALOG_INPUT_ERROR_MESSAGE), manager.getMessage(KEY_MESSAGE_DIALOG_INPUT_ERROR_TITLE), JOptionPane.WARNING_MESSAGE);
                textField.setText(null);
                e1.printStackTrace();
            }
            
        }
        
    }
    
}
